package com.hzw.saas.service.workspace.service;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.workspace.IUserWorkspaceService;
import com.hzw.saas.api.workspace.bo.UserWorkspaceBo;
import com.hzw.saas.common.config.util.PageParam;
import com.hzw.saas.common.config.util.PageUtils;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.common.util.SnowFlakeUtil;
import com.hzw.saas.service.workspace.mapper.UserWorkspaceMapper;
import com.hzw.saas.service.workspace.pojo.model.UserWorkspace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jodd.util.StringUtil;
import lombok.AllArgsConstructor;

/**
 * <p>
 * 用户工作空间表 服务实现类
 * </p>
 *
 * @author sonam
 * @since 2021-02-03
 */
@Service
@AllArgsConstructor
public class UserWorkspaceServiceImpl extends ServiceImpl<UserWorkspaceMapper, UserWorkspace> implements IUserWorkspaceService, IService<UserWorkspace> {

    @Override
    public boolean existUserWorkspace(String userId) {
        return this.count(Wrappers.<UserWorkspace>lambdaQuery()
            .eq(UserWorkspace::getUserId, userId)) > 0;
    }

    @Override
    public List<UserWorkspaceBo> listWorkspaceByUser(String userId) {
        if (StringUtils.isBlank(userId)) {
            return new ArrayList<>();
        }
        List<UserWorkspace> list = this.lambdaQuery().eq(UserWorkspace::getUserId, userId).list();
        return MapperUtil.nf().mapAsList(list, UserWorkspaceBo.class);
    }

    @Override
    public IPage<UserWorkspaceBo> pageWorkspaceByUser(PageParam pageParam, String userId) {
        IPage<UserWorkspace> page = this.page(pageParam.convert(), Wrappers.<UserWorkspace>lambdaQuery().eq(UserWorkspace::getUserId, userId));
        return PageUtils.convert(page, UserWorkspaceBo.class);
    }

    @Override
    public UserWorkspaceBo createUserWorkspace(UserWorkspaceBo userWorkspaceDto) {

        if(StringUtil.isBlank(userWorkspaceDto.getSpaceRoot())) {
            userWorkspaceDto.setSpaceRoot("/headless/Desktop/MySpace");
        }

        if(StringUtil.isBlank(userWorkspaceDto.getSpaceId())) {
            userWorkspaceDto.setSpaceId(SnowFlakeUtil.nextIdStr());
        }

        UserWorkspace userWorkspace = MapperUtil.nf().map(userWorkspaceDto, UserWorkspace.class);
        save(userWorkspace);

        return userWorkspaceDto;
    }

    @Override
    public UserWorkspaceBo getUserWorkspaceBySpaceId(String spaceId) {
        UserWorkspace userWorkspace = this.getOne(Wrappers.<UserWorkspace>lambdaQuery()
            .eq(UserWorkspace::getSpaceId, spaceId));
        UserWorkspaceBo userWorkspaceDto = MapperUtil.nf().map(userWorkspace, UserWorkspaceBo.class);
        return userWorkspaceDto;
    }

    @Override
    public boolean updateUserWorkspace(UserWorkspaceBo userWorkspaceDto) {
        UserWorkspace userWorkspace = MapperUtil.nf().map(userWorkspaceDto, UserWorkspace.class);
        return this.updateById(userWorkspace);
    }

    @Override
    @Transactional
    public boolean deleteUserWorkspaceById(String spaceId) {
        return this.removeById(spaceId);
    }
}
